<template>
  <cfDialog :dialogObj='dialogObj'>
    <div slot='content' v-loading="loading">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="72px">
        <el-row :gutter="20">
          <el-col :xs='24' :sm='24' :md='24' :lg='24' :xl='24'>
            <el-form-item label="上级菜单" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="menuOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='24' :md='24' :lg='24' :xl='24'>
            <el-form-item label="菜单类型" prop="menuType">
              <el-radio-group v-model="form.menuType">
                <el-radio label="M">目录</el-radio>
                <el-radio label="C">菜单</el-radio>
                <el-radio label="F">按钮</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType != 'F'">
            <el-form-item label="菜单图标" prop="icon">
              <el-popover
                placement="bottom-start"
                width="460"
                trigger="click"
                @show="$refs['iconSelect'].reset()"
              >
                <IconSelect ref="iconSelect" @selected="selected" :active-icon="form.icon" />
                <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
                  <svg-icon
                    v-if="form.icon"
                    slot="prefix"
                    :icon-class="form.icon"
                    style="width: 25px;"
                  />
                  <i v-else slot="prefix" class="el-icon-search el-input__icon" />
                </el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12'>
            <el-form-item label="菜单名称" label-width="80px" prop="menuName">
              <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12'>
            <el-form-item label="显示排序" label-width="80px" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType != 'F'">
            <el-form-item prop="isFrame" label-width="100px">
              <span slot="label">
                <el-tooltip content="选择是外链则路由地址需要以`http(s)://`开头" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                是否外链
              </span>
              <el-radio-group v-model="form.isFrame">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType != 'F'">
            <el-form-item prop="path" label-width="118px">
              <span slot="label">
                <el-tooltip content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由地址
              </span>
              <el-input v-model="form.path" placeholder="请输入路由地址" />
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType == 'C'">
            <el-form-item prop="component" label-width="100px">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                组件路径
              </span>
              <el-input v-model="form.component" placeholder="请输入组件路径" />
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType != 'M'">
            <el-form-item prop="perms" label-width="100px">
              <el-input v-model="form.perms" placeholder="请输入权限标识" maxlength="100" />
              <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermi('system:user:list')`)" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                权限字符
              </span>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType == 'C'">
            <el-form-item prop="query" label-width="100px">
              <el-input v-model="form.query" placeholder="请输入路由参数" maxlength="255" />
              <span slot="label">
                <el-tooltip content='访问路由的默认传递参数，如：`{"id": 1, "name": "ry"}`' placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由参数
              </span>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType == 'C'">
            <el-form-item prop="isCache" label-width="100px">
              <span slot="label">
                <el-tooltip content="选择是则会被`keep-alive`缓存，需要匹配组件的`name`和地址保持一致" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                是否缓存
              </span>
              <el-radio-group v-model="form.isCache">
                <el-radio label="0">缓存</el-radio>
                <el-radio label="1">不缓存</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType != 'F'">
            <el-form-item prop="visible" label-width="100px">
              <span slot="label">
                <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                显示状态
              </span>
              <el-radio-group v-model="form.visible">
                <el-radio
                  v-for="dict in sysShowHide"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='12' :md='12' :lg='12' :xl='12' v-if="form.menuType != 'F'">
            <el-form-item prop="status" label-width="100px">
              <span slot="label">
                <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                菜单状态
              </span>
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in sysNormalDisable"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template slot='footer'>
      <el-button type='primary' size='small' v-if="!loading" @click='handleConfirm'>确定</el-button>
    </template>
  </cfDialog>
</template>
<script>
import { listMenu, getMenu, addMenu, updateMenu } from "@/api/system/menu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
export default {
  components: { Treeselect, IconSelect },
  props: {
    dialogObj: {
      type: Object,
      default: ({
        visible: false,
        type: '',
        id: '',
        title: '',
        width: '520px'
      })
    }
  },
  data() {
    return {
      /**表单校验 */
      rules: {
        menuName: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "菜单顺序不能为空", trigger: "blur" }
        ],
        path: [
          { required: true, message: "路由地址不能为空", trigger: "blur" }
        ]
      },
      form: {},
      menuOptions: [], /**菜单树选项 */
      loading: false
    }
  },
  created() {
    /**获取字典数据 */
    this.getSysShowHide();
    this.getSysNormalDisable();

    this.getTreeselect();
    if(this.dialogObj.id) {
      this.getDetailFn();
    } else {
      this.form = {
        menuId: undefined,
        parentId: this.dialogObj.parentId,
        menuName: undefined,
        icon: undefined,
        menuType: "M",
        orderNum: undefined,
        isFrame: "1",
        isCache: "0",
        visible: "0",
        status: "0"
      }
    }
  },
  methods: {
    /** 查询菜单下拉树结构 */
    async getTreeselect() {
      this.menuOptions = [];
      const res = await listMenu();
      if(res.code == 200) {
        const menu = { menuId: 0, menuName: '主类目', children: [] };
        menu.children = this.handleTree(res.data, "menuId");
        this.menuOptions.push(menu);
      }
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
    /**选择图标 */
    selected(name) {
      this.form.icon = name;
    },
    /**确定按钮提交功能*/
    handleConfirm() {
      this.submitForm('form', async ()=> {
        this.loading = true;
        let res;
        if (this.dialogObj.id) {
          res = await updateMenu(this.form);
        } else {
          res = await addMenu(this.form);
        }
        if(res.code == 200) {
          this.$message.success('操作成功');
          this.dialogObj.visible = false;
          this.$emit('refresh');
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      });
    },
    /**获取弹窗详情*/
    async getDetailFn() {
      this.loading = true;
      const res = await getMenu(this.dialogObj.id);
      if(res.code == 200) {
        this.form = res.data;
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    }
  }
}
</script>